package com.yh.correct.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yh.correct.dao.IAccompliceMapper;
import com.yh.correct.dao.ICorrectedPersonMapper;
import com.yh.correct.dao.IFileAttachmentsMapper;
import com.yh.correct.dao.ISocialRelationsMapper;
import com.yh.correct.model.Accomplice;
import com.yh.correct.model.CorrectedPerson;
import com.yh.correct.model.FileAttachments;
import com.yh.correct.model.JudicialDeptment;
import com.yh.correct.model.Page;
import com.yh.correct.model.SocialRelations;
import com.yh.correct.service.ICorrectedPersonService;

/**
 * 作者: 王卫 创建时间:2017-03-07 描述:矫正人员业务实现类
 */
@Service
public class CorrectedPersonServiceImpl implements ICorrectedPersonService {
	/**
	 * 使用logger4j日志
	 */
	private static Logger logger = LogManager.getLogger(CorrectedPersonServiceImpl.class);
	/**
	 * 自动注入
	 */
	@Autowired
	private ICorrectedPersonMapper correctedPersonMapper;
	@Autowired
	private IAccompliceMapper accompliceMapper;
	@Autowired
	private ISocialRelationsMapper socialRelationsMapper;
	@Autowired
	private IFileAttachmentsMapper fileAttachmentsMapper;
	/**
	 * 获取矫正人员
	 */
	@Override
	public CorrectedPerson getCorrectedPerson(CorrectedPerson correctedperson) {
		CorrectedPerson correctedPerson=null;
		try {
			correctedPerson = correctedPersonMapper.getCorrectedPerson(correctedperson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return correctedPerson;
	}
	/**
	 * 添加矫正人员
	 */
	@Override
	public int addCorrectedPerson(CorrectedPerson correctedperson) throws Exception {
		logger.debug("添加入矫人员信息：" + correctedperson);
		return correctedPersonMapper.addCorrectedPerson(correctedperson);
	}
	/**
	 * 根据id删除矫正人员
	 */
	@Override
	public int delCorrectedPerson(CorrectedPerson correctedperson) {
		int i = 0;
		try {
			i = correctedPersonMapper.delCorrectedPerson(correctedperson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}
	/**
	 * 根据id修改矫正人员信息
	 */
	@Override
	public int editCorrectedPerson(CorrectedPerson correctedperson) throws Exception {
		return correctedPersonMapper.editCorrectedPerson(correctedperson);
	}
	/**
	 * 模糊分页+动态条件查询,返回page
	 */
	@Override
	public Page getCorrectedPersonByPage(Map<String, Object> map) {
		Page page = new Page();
		try {
			int curPageNum = Integer.parseInt(String.valueOf(map.get("curPageNum"))); // 当前页码
			page.setCurPageNum(curPageNum);
			int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize"))); // 页面大小
			page.setPageSize(pageSize);
			int totalNum=correctedPersonMapper.findCount(map);
			page.setTotalPage(totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1); //总页数
			page.setResult(correctedPersonMapper.findCorrectedPersonByPage(map)); //结果集
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("出现异常!");
		}
		return page;
	}
	/**
	 * 分页条件查询
	 */
	public Page getLikeCorrectedPerson(Map<String, Object> condition) {
		Page page = new Page();
		try {
			page.setTotalNum(correctedPersonMapper.getLikeCorrectedPersonCount(condition));
			int pageSize = Integer.parseInt(String.valueOf(condition.get("pageSize")));
			page.setPageSize(pageSize);
			int curPageNum = Integer.parseInt(String.valueOf(condition.get("pageNum")));
			page.setCurPageNum(curPageNum);
			page.setTotalPage(page.getTotalNum() / pageSize + (page.getTotalNum() % pageSize == 0 ? 0 : 1));
			page.setResult(correctedPersonMapper.getLikeCorrectedPerson(condition));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}

	@Override
	public PageInfo<Object> getPersion(Map<String, Object> map) throws Exception {
		PageHelper.startPage((int)map.get("pageNum"), (int)map.get("pageSize"), true);
		PageInfo<Object> pageInfo=new PageInfo<Object>(correctedPersonMapper.getPersion(map));
		return pageInfo;
	}

	/**
	 * 导出Excel 查询所有
	 */
	@Override
	public List<Map<String, Object>> getAllCorrectedPerson() {
		return correctedPersonMapper.getAllCorrectedPerson();
	}
	/**
	 * 导出excel
	 */
	public List<CorrectedPerson> findCorrectedPerson() {
		List<CorrectedPerson> list = null;
		try {
			list = correctedPersonMapper.findCorrectedPerson();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 修改矫正人员,传值map
	 */
	@Override
	public int updateCorrectedPerson(Map<String, Object> map)  {
		int i=0;
		try {
			i = correctedPersonMapper.updateCorrectedPerson(map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	/**
	 * 获取入矫人员所有信息
	 */
	@Override
	public Map<String, Object> getAll(CorrectedPerson correctedperson) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		CorrectedPerson correctedperson1 = correctedPersonMapper.getCorrectedPerson(correctedperson);
		int i = correctedperson.getId();
		List<Accomplice> list1 = accompliceMapper.getAccomplice(i);//
		List<SocialRelations> list2 = socialRelationsMapper.getSocialRelations(i);//
		List<FileAttachments> list3 = fileAttachmentsMapper.getFileAttachments(i);//
		int j = correctedperson1.getJid();
		map.put("judicialDeptment", getUnitById(j));
		map.put("correctedperson", correctedperson1);
		map.put("judicialDeptment1", correctedPersonMapper.getAllUnit()); /*comcn155 获得corrected表中的数据*/
		map.put("accolist", list1);
		map.put("socilist", list2);
		map.put("filelist", list3);
		return map;
	}
	/**
	 * 获取所有的矫正单位
	 */
	@Override
	public List<JudicialDeptment> getAllUnit() {
		// TODO Auto-generated method stub
		return correctedPersonMapper.getAllUnit();
	}
	/**
	 * 删除入矫人员详细信息
	 */
	@Override
	public void delAll(CorrectedPerson correctedperson) throws Exception {
		correctedPersonMapper.delCorrectedPerson(correctedperson);
		accompliceMapper.delAccomplice(correctedperson.getId());
		socialRelationsMapper.delSocialRelations(correctedperson.getId());
		fileAttachmentsMapper.delFileAttachments(correctedperson.getId());
	}
	/**
	 * 增加入矫人员详细信息
	 */
	@Override
	@Transactional
	public void addAll(CorrectedPerson correctedperson,List<SocialRelations> socilist,List<Accomplice> accolist,List<FileAttachments> filelist) throws Exception {
		correctedPersonMapper.addCorrectedPerson(correctedperson);
		int id=correctedperson.getId();
		for(SocialRelations sr:socilist){
			sr.setPid(id);
			socialRelationsMapper.addSocialRelations(sr);
		}
		for(Accomplice ap:accolist){
			ap.setPid(id);
			accompliceMapper.addAccomplice(ap);
		}
		for(FileAttachments fa:filelist){
			fa.setPid(id);
			fileAttachmentsMapper.addFileAttachments(fa);
		}
	}
	/**
	 * 修改入矫人员详细信息
	 */
	@Override
	@Transactional
	public void editAll(CorrectedPerson correctedperson,List<SocialRelations> socilist,List<Accomplice> accolist,List<FileAttachments> filelist) throws Exception {
		correctedPersonMapper.editCorrectedPerson(correctedperson);
		if(socilist!=null){
			for(SocialRelations sr:socilist){
				socialRelationsMapper.editSocialRelations(sr);
			}
		}
		if(accolist!=null){
			for(Accomplice ap:accolist){
				accompliceMapper.editAccomplice(ap);
			}
		}
		if(filelist!=null){	
			for(FileAttachments fa:filelist){
				fileAttachmentsMapper.editFileAttachments(fa);
			}
		}
	}
	/**
	 * 批量删除
	 */
	@Override
	public void deleteBatch(List<Integer> temp) {
		try {
			correctedPersonMapper.deleteBatch(temp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public JudicialDeptment getUnitById(int jid) {
		// TODO Auto-generated method stub
		return correctedPersonMapper.getUnitById(jid);
	}
	/**
	 * 执行类别
	 */
	@Override
	public List<String> findType() {
		List<String> list = null;
		try {
			list = correctedPersonMapper.getType();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 犯罪类别
	 */
	@Override
	public List<String> findCrimeTypes() {
		List<String> list = null;
		try {
			list = correctedPersonMapper.getCrimeTypes();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 原判期刑
	 */
	@Override
	public List<String> findSentenceDate() {
		List<String> list = null;
		try {
			list = correctedPersonMapper.getSentenceDate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 根据id获取矫正人员及其矫正单位(用于批量导出)
	 */
	@Override
	public Map<String, Object> getCorrectedPersonById(int id){
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			map = correctedPersonMapper.findCorrectedPersonById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 获取矫正人员及其矫正单位(用于全部导出)
	 */
	@Override
	public List<Map<String, Object>> getAllCorrectedPersonnel(Integer jid){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = correctedPersonMapper.findAllCorrectedPersonnel(jid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 查询所有矫正人员详细信息
	 * @author comcn155
	 * @param correctedperson
	 * @return
	 * @throws Exception
	 */
	public CorrectedPerson getCorrectedPersonC(CorrectedPerson correctedperson) throws Exception{
		return correctedPersonMapper.getCorrectedPerson(correctedperson);
	};
}
